package de.rj.urwalking.loader;

import java.util.ArrayList;

import android.content.Context;
import android.location.Location;

import com.google.android.gms.maps.model.LatLng;
import com.threed.jpct.SimpleVector;

import de.ralf.urwalking.soplugin.EPSGPoint;
import de.rj.urwalking.db.DBSalientObject;
import de.rj.urwalking.db.ReadWritePostgreSQL;
import de.rj.urwalking.util.GeoUtil;

/**
 * Used to load previously added salient objects from the database in a separate thread.
 * 
 * @author rjj
 * 
 */
public class SalientObjectLoader extends AsyncLoader<ArrayList<DBSalientObject>> {

	private float mOrientation;

	/** EPSG31468-Hochwert */
	private double northing;
	/** EPSG31468-Rechtswert */
	private double easting;

	public SalientObjectLoader(Context context, SimpleVector position, float orientation) {
		super(context);
		this.mOrientation = orientation;

		northing = position.y;
		easting = position.x;
	}

	public SalientObjectLoader(Context context, LatLng position, float orientation) {
		super(context);
		this.mOrientation = orientation;

		// convert GPS coordinates to EPSG 31468, which is used in the database
		EPSGPoint epsgPoint = GeoUtil.convertLatLonToEPSG31468(position.longitude, position.latitude);
		northing = epsgPoint.getY();
		easting = epsgPoint.getX();
	}

	public SalientObjectLoader(Context context, Location location, float orientation) {
		this(context, new LatLng(location.getLatitude(), location.getLongitude()), orientation);
	}

	@Override
	public ArrayList<DBSalientObject> loadInBackground() {

		ReadWritePostgreSQL dbWrapper = new ReadWritePostgreSQL();
		ArrayList<DBSalientObject> salientObjects = dbWrapper.fetchSalientObjects(new double[] { northing, easting },
				mOrientation);
		dbWrapper.closeConnection();

		return salientObjects;
	}
}
